package pers.cj.pdos.controller.file.file;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import pers.cj.pdos.utils.FileProperUtil;
import pers.cj.pdos.utils.FileUtils;
import pers.cj.pdos.utils.Utils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * 文件下载器
 */
@Controller
@RequestMapping("/file")
public class FileController {

    /**
     * 导出临时文件下载器
     * @param fileName - 临时文件
     * @param response
     * @throws IOException
     */
    @RequestMapping("/excel/temp/{fileName:.+}")
    public void downExcel(@PathVariable("fileName")String fileName, HttpServletResponse response) throws IOException {
        if (Utils.isEmpty(fileName)){
            response.getWriter().println("文件名不能为空");
            return;
        }
        try {
            String filename = FileProperUtil.getInstance().getVal("excel.temp") + fileName;
            byte[] file = FileUtils.read(filename);
            responseExcel(fileName, file, response);
        } catch (IOException exception) {
            response.getWriter().println("未找到文件");
        }
    }

    /**
     * 商品导入模板下载器
     * @param response
     * @throws IOException
     */
    @RequestMapping("/excel/goods/template")
    public void downGoodsTemplate(HttpServletResponse response) throws IOException {
        try {
            InputStream inputStream = getClass().getResourceAsStream("/template/goodsImportTemplate.xlsx");
            byte[] file = FileUtils.read(inputStream);
            responseExcel("商品导入模板.xlsx", file, response);
        } catch (IOException exception) {
            response.getWriter().println("未找到文件");
        }
    }

    /**
     * 客户导入模板下载器
     * @param response
     * @throws IOException
     */
    @RequestMapping("/excel/custom/template")
    public void downCustomTemplate(HttpServletResponse response) throws IOException {
        try {
            InputStream inputStream = getClass().getResourceAsStream("/template/customImportTemplate.xlsx");
            byte[] file = FileUtils.read(inputStream);
            responseExcel("客户导入模板.xlsx", file, response);
        } catch (IOException exception) {
            response.getWriter().println("未找到文件");
        }
    }

    /**
     * 供应商导入模板下载器
     * @param response
     * @throws IOException
     */
    @RequestMapping("/excel/provider/template")
    public void downProviderTemplate(HttpServletResponse response) throws IOException {
        try {
            InputStream inputStream = getClass().getResourceAsStream("/template/providerImportTemplate.xlsx");
            byte[] file = FileUtils.read(inputStream);
            responseExcel("供应商导入模板.xlsx", file, response);
        } catch (IOException exception) {
            response.getWriter().println("未找到文件");
        }
    }



    private void responseExcel(String fileName, byte[] file, HttpServletResponse response) throws IOException {
        response.setHeader("Content-Disposition", "attachment;filename*=UTF-8''" + URLEncoder.encode(fileName,"UTF-8"));
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("UTF-8");
        OutputStream os = response.getOutputStream();
        os.write(file);
        os.flush();
        os.close();
    }
}
